import "@/axios/axiosConfig"
import router
  from './routes/route';
import './MavonEditor'
import 'font-awesome/css/font-awesome.css'
import store from '@/store/store'
import { mapActions, mapMutations, mapState } from 'vuex';
import statusCode from '@/commons/statusCode'
import userApi from './axios/userApi';
import { format } from 'date-fns'
import avatar from '@/components/CommonItems/avatar.vue'
import MyTime from './filters/MyTime';
import adminApi from "./axios/adminApi";
import Vue from 'vue'
import App from './App.vue'

Vue.config.productionTip = false
import {
  Message, Menu, MenuItem, Submenu, Header, Container, Row, Col
  , Input, Avatar, Button, Popover, Badge, Main, Aside, Tabs, TabPane,
  Form, FormItem, Select, DatePicker, TimePicker, Switch, CheckboxGroup, Checkbox,
  Radio, Upload, RadioGroup, Option, Card, Timeline, TimelineItem,
  Image, Statistic, PageHeader, Divider, Table, TableColumn, Pagination, Dialog,
  MessageBox, Loading, InfiniteScroll, Backtop, Skeleton, SkeletonItem, Link, Popconfirm, Tag, Dropdown, DropdownItem, DropdownMenu, Empty,

} from "element-ui";


Vue.use(Menu);
Vue.use(Submenu)
Vue.use(MenuItem)
Vue.use(Header)
Vue.use(Container)
Vue.use(Row)
Vue.use(Col)
Vue.use(Input)
Vue.use(Avatar)
Vue.use(Button)
Vue.use(Popover)
Vue.use(Badge)
Vue.use(Main)
Vue.use(Aside)
Vue.use(Tabs)
Vue.use(TabPane)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Input)
Vue.use(Select)
Vue.use(DatePicker)
Vue.use(TimePicker)
Vue.use(Switch)
Vue.use(CheckboxGroup)
Vue.use(Checkbox)
Vue.use(Radio)
Vue.use(Upload)
Vue.use(RadioGroup)
Vue.use(Option)
Vue.use(Card)
Vue.use(Timeline)
Vue.use(TimelineItem)
Vue.use(Image)
Vue.use(Statistic)
Vue.use(PageHeader)
Vue.use(Divider)
Vue.use(Table)
Vue.use(TableColumn)
Vue.use(Pagination)
Vue.use(Dialog)
Vue.use(Skeleton)
Vue.use(SkeletonItem)
Vue.use(Popconfirm)
Vue.directive('Loading', Loading)
Vue.directive("infinite-scroll", InfiniteScroll)
Vue.use(Backtop)
Vue.use(Link)
Vue.use(Tag)
Vue.use(Dropdown)
Vue.use(DropdownItem)
Vue.use(DropdownMenu)
Vue.use(Empty)
Vue.prototype.$message = Message;
Vue.prototype.$loading = Loading.service;
Vue.prototype.$msgbox = MessageBox;
Vue.prototype.$alert = MessageBox.alert;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$prompt = MessageBox.prompt;
Vue.prototype.$notify = Notification;
Vue.prototype.$message = Message;
Vue.prototype.$status = statusCode



Vue.component('avatar', avatar)
// 自定义过滤器
Vue.filter('dateFormat', function (value, formatStr = 'yyyy-MM-dd HH:mm:ss') {
  if (!value) return
  return format(new Date(value), formatStr)
})

Vue.filter('MyTime', MyTime)
Vue.mixin({
  computed: {
    ...mapState('user', { userInfo: 'info', isLogin: 'isLogin' }),
    ...mapState(['isLoginDialog', 'commentInputCid']),
    ...mapState('comment', ['commentList']),
    ...mapState('banDialog', ['banDialogData', 'isBanDialogActive']),
  },
  methods: {
    ...mapMutations('user', ['login', 'logout']),
    ...mapMutations(['popLoginDialog', 'closeLoginDialog', 'changeCommentInput']),
    ...mapActions("user", ["updateUser"]),
    ...mapMutations("comment", ["purgeCommentList",
      "deleteCommentById",
      "initCommentList", "appendComments"]),
    ...mapMutations('banDialog', ['popBanDialog', 'closeBanDialog']),

  },

})
router.beforeEach((to, from, next) => {
  if (to.meta.title)
    document.title = to.meta.title
  if (to.matched.some(v => v.meta.requireLogin)) {
    if (!store.state.user.isLogin) {
      Message.error("请登录后再进行操作")
      return next(false)
    }
    userApi.getUserInfo()
      .then(({ data }) => {
        store.commit("user/login", data.data)
        if (to.meta.requireType){
          return adminApi.getAuthInfo({ forumId: to.params.forumId })}
        next()
      })
       .then(({ data }) => {
        console.log("here");
        to.meta.auth=data.data
        if (to.meta.requireType =='admin' && (data.data == 'admin' || data.data == 'owner'))
          return next()
        else if (to.meta.requireType == 'owner' && data.data == 'owner')
          return next()
        Message.warning('无权限!')
        return next({ name: 'index' })
      });
  }
  else
    next()
})



new Vue({
  store,
  router,
  render: h => h(App),
  created() {
    if (this.isLogin)
      userApi.getUserInfo()
        .then(({ data }) => {
          store.commit("user/login", data.data)
        })
  }
}).$mount('#app')
